import * as Cesium from 'cesium'

export default class Pyramid {
    constructor(viewer, position, angle1, angle2, distance) {
        this.viewer = viewer;

        this.position = position ;

        this.angle1 = angle1 ? angle1 : Cesium.Math.toRadians(60.0);//前后角度
        this.angle2 = angle2 ? angle2 : Cesium.Math.toRadians(90.0);//左右角度
        this.distance = distance ? distance : 2000000.0;//远平面距离
        this.headingNum = 0;
        this.pitchNum = Cesium.Math.PI;
        this.rollNum = 0;
        this.color = new Cesium.Color(1.0, 0.0, 0.0, 0.5);

        this.frustum = null;
        this.frustumPrimitive = null;

    }

    
    createFrustum(){
        this.frustum = new Cesium.PerspectiveFrustum({
            fov: this.angle1,//角度
            aspectRatio: this.angle2/this.angle1,//比例
            near: 10.0,//近平面
            far: this.distance,//远平面
        });
    }
    //
    renderFrustum(){
        const hpr = new Cesium.HeadingPitchRoll(this.headingNum, this.pitchNum, this.rollNum) // 生成四元数
        const orientation = Cesium.Transforms.headingPitchRollQuaternion(this.position, hpr) // 设置姿态
        
        this.createFrustum();
        this.frustumPrimitive = this.viewer.scene.primitives.add(
            new Cesium.Primitive({
            geometryInstances: new Cesium.GeometryInstance({
                geometry: new Cesium.FrustumGeometry({
                    frustum: this.frustum,
                    origin: this.position,
                    orientation: orientation,
                }),
                attributes: {
                color: Cesium.ColorGeometryInstanceAttribute.fromColor(
                    this.color
                ),
                },
            }),
            appearance: new Cesium.PerInstanceColorAppearance({
                flat: true,
            }),
            })
        );
        // this.frustumOutlinePrimitive = this.viewer.scene.primitives.add(
        //     new Cesium.Primitive({
        //     geometryInstances: new Cesium.GeometryInstance({
        //         geometry: new Cesium.FrustumOutlineGeometry({
        //             frustum: this.frustum,
        //             origin: this.position,
        //             orientation: orientation,
        //         }),
        //         attributes: {
        //         color: Cesium.ColorGeometryInstanceAttribute.fromColor(
        //             new Cesium.Color(0.0, 0.0, 0.0, 1.0)
        //         ),
        //         },
        //     }),
        //     appearance: new Cesium.PerInstanceColorAppearance({
        //         flat: true,
        //     }),
        //     })
        // );
    }
    
    changeFrustum(angle1, angle2, distance, headingNum, pitchNum, rollNum, color){
        //更新参数
        this.angle1 = angle1;//前后角度
        this.angle2 = angle2;//左右角度
        this.distance = distance;//远平面距离
        this.headingNum = headingNum;
        this.pitchNum = pitchNum;
        this.rollNum = rollNum;
        this.color = color;
        //删除
        this.removePyramid()
        //重绘
        this.renderFrustum();
    }

    removePyramid(){
        this.viewer.scene.primitives.remove(this.frustumPrimitive);
    }
}